/*
    Copyright (c) 2010, Kozak Paweł (haango.mbox@gmail.com)
    All rights reserved.


    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        * Neither the name of the <organization> nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef LaunchManager_H
#define LaunchManager_H

#include <QObject>
#include <QSqlDatabase>
#include <stdint.h>
#include <QString>

/**
  * Menadżer Launcherów odpowiada za zarządzanie informacjami o launcherach
  * przy zastosowaniu w tym celu bazy danych.
  *
  * Poza faktem zarządzania informacjami, menadżer pozwala również na
  * uruchomienie podanego launchera.
  *
  * Klasa posiada wiele metod, które nie sprawdzają wprowadzonych parametrów.
  * Dlatego też klasa powinna otrzymywać odpowiednio przygotowane dane.
  *
  * Metody jakie zostały do zaimplementowania to:
  * 1. pobranie dostępnych laucherów dla QCompleter'a
  * 2. pobranie wszystkich informacji dla panelu edycji
  *
  * Metody zostaną wykonane, gdy poznam bardziej front-end aplikacji.
  *
  *
  * Opis każdej metody znajduje się w pliku implementacji.
  */
class LaunchManager : public QObject {
    Q_OBJECT

public:
    ~LaunchManager();
    
    void showExample();
    
    bool prepare();
    bool restoreState();
    
    bool launch( const QString & _launcher );
    
    bool addLaunchers( const QStringList & _launchers, const QString & _fileName, const QString & _icon = "");
    bool addLauncher( const QString & _launcher, const int32_t & _idProgram );
    int32_t getIdProgram( const QString & _fileName );
    
    bool editFileName( const QString & _fileName, const QString & _newFileName);
    bool editIcon( const QString & _fileName, const QString & _newIcon);
    bool editLauncher( const QString & _launcher, const QString & _newLauncher);
    
    bool removeProgram( const QString & _fileName );
    bool removeAllProgramLauncher( const int32_t _idProgram );
    bool removeLauncher(const QString & _launcher);

private:
    bool connectDatabase();
    bool createAllTables();

    bool resetDatabase();
    
    bool addProgram( const QString & _fileName, const QString & _icon = "" );
    
    bool isExistsDriver();
    bool isEmptyDatabase(); 
    bool isExistsFileName(const QString & _fileName, bool & _isError);
    bool isExistsLauncher(const QString & _launcher, bool & _isError);
    int32_t countCreatedTables();
    
signals:
    void error( const QString & _message );

private:
    // Baza danych.
    QSqlDatabase database;
    
    // oczekiwana ilość tablic w bazie danych
    static const int32_t expectedTablesCount = 2;
    
    // sterownik do bazy danych
    static const QString driver;
    
    // nazwa pliku bazy danych
    static const QString databaseFileName;
};

#endif // LaunchManager_H
